import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVC
from sklearn.metrics import classification_report, accuracy_score, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns

# 读取CSV文件（修正文件路径）
data = pd.read_csv('./data/情感文本分析/sentimentdataset.csv')

# 数据介绍
print(f"数据总量: {len(data)}条记录")
sentiment_counts = data['Sentiment'].value_counts()
print(f"各情感类别分布:\n{sentiment_counts}")

# 文本预处理
text_data = data['Text']
sentiment_labels = data['Sentiment']

# 词汇表示：使用TF-IDF向量化器
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(text_data)

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, sentiment_labels, test_size=0.3, random_state=42)

# 文本分类：使用线性SVM分类器
clf = LinearSVC(random_state=42, dual=False)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)

# 输出分类报告
print(classification_report(y_test, y_pred))

# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print(f"SVM模型准确率: {accuracy:.4f}")

# 混淆矩阵
cm = confusion_matrix(y_test, y_pred)
print(f"混淆矩阵:\n{cm}")

# 复杂的可视化图表展示结果
sns.heatmap(cm, annot=True, cmap='coolwarm', fmt='d')
plt.title('Sentiment Classification Confusion Matrix')
plt.xlabel('Predicted Sentiment')
plt.ylabel('Actual Sentiment')
plt.show()


# 数据预处理：仅保留文本列，并简单提取摘要
data['Text'] = data['Text'].str.split('.').str[:3].apply(lambda x: '. '.join([sent + '.' for sent in x]))
sentiment_labels = data['Sentiment']
text_data = data['Text']

# 词汇表示：使用TF-IDF向量化器
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(text_data)

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, sentiment_labels, test_size=0.3, random_state=42)

# 文本分类：使用线性SVM分类器
clf = LinearSVC(random_state=42, dual=False)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)

# 分析和预测SVM结果的好坏及误差
accuracy = accuracy_score(y_test, y_pred)
print(f"SVM模型准确率: {accuracy:.4f}")
print(classification_report(y_test, y_pred))

# 混淆矩阵可视化
cm = confusion_matrix(y_test, y_pred)
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]  # 归一化
plt.figure(figsize=(10, 8))
sns.heatmap(cm, annot=True, cmap='Blues', fmt='.2f')
plt.xlabel('Predicted Sentiment')
plt.ylabel('Actual Sentiment')
plt.title('Sentiment Classification Confusion Matrix')
plt.show()
